package guda.house.biz.security;

import guda.house.biz.beans.vo.VillageVO;
import guda.house.biz.enums.RoleEnum;

import java.util.List;

/**
 * Created by well on 15/10/15.
 */
public class AuthUser implements java.io.Serializable {

    private static final ThreadLocal<AuthUser> authHolder = new ThreadLocal<AuthUser>();

    private Long userId;
    private String[] roles;
    private String loginName;
    private String phone;

    private String companyName;
    private boolean firstLogin;
    private List<Long> villageIdList;
    private List<VillageVO> villageVOList;
    private List<String> resourceList;

    private boolean fromWeixin;


    public List<String> getResourceList() {
        return resourceList;
    }

    public void setResourceList(List<String> resourceList) {
        this.resourceList = resourceList;
    }

    public static final AuthUser getCurrentUser() {
        return authHolder.get();
    }

    public static final void setCurrentUser(AuthUser buyer) {
        authHolder.set(buyer);
    }

    public static final void cleanThreadLocal() {
        authHolder.remove();
    }

    public static boolean isLogin(){
        return authHolder.get().getUserId()!=null && !authHolder.get().isFromWeixin();
    }

    public boolean isFromWeixin() {
        return fromWeixin;
    }

    public void setFromWeixin(boolean fromWeixin) {
        this.fromWeixin = fromWeixin;
    }

    /**
     * 创建匿名用户。
     */
    public AuthUser() {

    }

    public boolean isFeeAdmin(){
        if(roles == null){
            return false;
        }
        for(String s:roles){
            if(RoleEnum.admin_fee.name().equals(s)){
                return true;
            }
        }
        return false;
    }

    public static ThreadLocal<AuthUser> getAuthHolder() {
        return authHolder;
    }

    public List<VillageVO> getVillageVOList() {
        return villageVOList;
    }

    public void setVillageVOList(List<VillageVO> villageVOList) {
        this.villageVOList = villageVOList;
    }

    public List<Long> getVillageIdList() {
        return villageIdList;
    }

    public void setVillageIdList(List<Long> villageIdList) {
        this.villageIdList = villageIdList;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public String[] getRoles() {
        return roles;
    }

    public void setRoles(String[] roles) {
        this.roles = roles;
    }

    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    public boolean isFirstLogin() {
        return firstLogin;
    }

    public void setFirstLogin(boolean firstLogin) {
        this.firstLogin = firstLogin;
    }
}
